package cn.ac.iie.kgmanager.controller;

import cn.ac.iie.kgmanager.domain.Graph;
import cn.ac.iie.kgmanager.domain.Node;
import cn.ac.iie.kgmanager.domain.Relation;
import cn.ac.iie.kgmanager.dto.request.EasyGraphRequest;
import cn.ac.iie.kgmanager.dto.request.GraphRequest;
import cn.ac.iie.kgmanager.dto.response.Response;
import cn.ac.iie.kgmanager.service.GraphService;
import cn.ac.iie.kgmanager.service.NodeService;
import cn.ac.iie.kgmanager.service.RelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @ClassName GraphController
 * @Author tjh
 * @Date 19/6/12 上午10:04
 * @Version 1.0
 **/
@RestController
public class GraphController {
    @Autowired
    GraphService graphService;
    @Autowired
    RelationService relationService;
    @Autowired
    NodeService nodeService;

    // 根据一个节点获取周边N层的图（完整版）
    @PostMapping("/graph")
    public Response getGraph(@RequestBody GraphRequest graphRequest) {
        String sourceId = graphRequest.getSourceId();
        if (StringUtils.isBlank(sourceId)) {
            return Response.paramError("sourceId不能为空");
        }
        // 根据关系获取图中所有节点
        List<Node> allNodes = graphService.getIdsByRelation(sourceId, graphRequest.getRelations());
        // 获取所有节点ID
        List<String> allIds = new LinkedList<>();
        for (Node node : allNodes) {
            allIds.add(node.getId());
        }

        // 根据节点ID获取所有关系
        List<Relation> allRelations = relationService.getRelationByNodes(allIds.toArray(new String[allIds.size()]));

        Graph graph = new Graph();
        graph.setNodes(new HashSet<>(allNodes));
        graph.setRelations(new HashSet<>(allRelations));
        return Response.operateSucessAndHaveData(graph);
    }

    // 根据一个节点获取周边N层的图（土拨鼠版）
    @PostMapping("/graph/easy")
    public Response getGraph(@RequestBody EasyGraphRequest easyGraphRequest) {
        String sourceId = easyGraphRequest.getSourceId();
        if (StringUtils.isBlank(sourceId)) {
            return Response.paramError("sourceId不能为空");
        }
        List<Node> allNodes = graphService.getIdsByRelation(sourceId, easyGraphRequest.getFirstLabels(), easyGraphRequest.getSecondLabels());

        // 获取所有节点ID
        List<String> allIds = new LinkedList<>();
        for (Node node : allNodes) {
            allIds.add(node.getId());
        }

        // 根据节点ID获取所有关系
        List<Relation> allRelations = relationService.getRelationByNodes(allIds.toArray(new String[allIds.size()]));

        Graph graph = new Graph();
        graph.setNodes(new HashSet<>(allNodes));
        graph.setRelations(new HashSet<>(allRelations));
        return Response.operateSucessAndHaveData(graph);
    }
   
}
